package com.cloudptt.wp.ui.more;

import com.cloudptt.api.product.Product;
import com.cloudptt.wp.R;
import com.cloudptt.wp.ScreenManager;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.more.advanced.CustomPositioningActivity;
import com.cloudptt.wp.ui.more.advanced.LanguageSelection;
import com.cloudptt.wp.ui.more.advanced.LogSettingActivity;
import com.cloudptt.wp.ui.more.advanced.UrgencyMsgSettingActivity;
import com.cloudptt.wp.ui.more.advanced.VoiceSettingActivity;
import com.mcptt.config.local.SysConfig;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

public class AdvancedSettingActivity extends PreferenceActivity implements OnPreferenceClickListener, OnPreferenceChangeListener, OnClickListener{

	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor editor;
	
	private Preference mVoicePref;
	private Preference mLocationPref;
	private Preference mLanguagePref;
	private Preference mLogPref;
	private Preference mEmergencyPref;
	
	private SwitchPreference mShowPttPref;
	private boolean isShowPtt;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Window window = getWindow();
		window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
		window.setStatusBarColor(getResources().getColor(R.color.app_theme_color_primary));
		super.onCreate(savedInstanceState);
		getActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.app_theme_color_primary));
		getActionBar().hide();
		setContentView(R.layout.advanced_setting_layout);
		ImageView iv = (ImageView) findViewById(R.id.back_up_icon);
		iv.setOnClickListener(this);
		TextView tv = (TextView) findViewById(R.id.title);
		tv.setText(R.string.advanced_setting);
		addPreferencesFromResource(R.xml.pref_advanced);
		initView();
		setLabelVal();
		ScreenManager.getScreenManager().pushActivity(this);
		
		sharedPreferences = getSharedPreferences("ptt_config", Context.MODE_PRIVATE);
		editor = sharedPreferences.edit();
		isShowPtt = sharedPreferences.getBoolean("showptt", false);
	}
	
	private void setLabelVal()
	{
		SysApplication app = ((SysApplication) getApplicationContext());
		Product product = app.getProduct();
		String lanStr = product.getSysConfig().getCurLan();
		
		if(lanStr.equals(SysConfig.LAN_EN_US))
		{
			mLanguagePref.setSummary("English");
		}else if(lanStr.equals(SysConfig.LAN_ZH_CN))
		{
			mLanguagePref.setSummary("中文");
		}
		else
		{
			mLanguagePref.setSummary(getString(R.string.system_language));
		}
	}
	
	private void setPttVal(){
		mShowPttPref.setChecked(isShowPtt);
	}
	
	@Override
	protected void onResume() {
		setLabelVal();
		setPttVal();
		super.onResume();
	}

	private void initView() {
		
		mVoicePref = findPreference("voice_settings");
		mLocationPref = findPreference("location_settings");
		mLanguagePref = findPreference("language_settings");
		mLogPref = findPreference("log_settings");
		mEmergencyPref = findPreference("urgency_settings");
		mShowPttPref = (SwitchPreference) findPreference("show_ptt");
		
		mVoicePref.setOnPreferenceClickListener(this);
		mLocationPref.setOnPreferenceClickListener(this);
		mLanguagePref.setOnPreferenceClickListener(this);
		mLogPref.setOnPreferenceClickListener(this);
		mEmergencyPref.setOnPreferenceClickListener(this);
		mShowPttPref.setOnPreferenceChangeListener(this);
		
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		
		if("voice_settings".equals(preference.getKey())){
			Intent intent = new Intent(AdvancedSettingActivity.this, VoiceSettingActivity.class);
            startActivity(intent);
            return true;
			
		}else if("location_settings".equals(preference.getKey())){
			Intent intent = new Intent(AdvancedSettingActivity.this, CustomPositioningActivity.class);
            startActivity(intent);
			return true;
			
		}else if("language_settings".equals(preference.getKey())){
			Intent intent = new Intent(AdvancedSettingActivity.this, LanguageSelection.class);
            startActivity(intent);
			return true;
			
		}else if("log_settings".equals(preference.getKey())){
			Intent intent = new Intent(AdvancedSettingActivity.this, LogSettingActivity.class);
            startActivity(intent);
			return true;
			
		}else if("urgency_settings".equals(preference.getKey())){
			Intent intent = new Intent(AdvancedSettingActivity.this, UrgencyMsgSettingActivity.class);
            startActivity(intent);
			return true;
			
		}
		
		return false;
	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object newValue) {
		// TODO Auto-generated method stub
		if(preference.getKey().equals("show_ptt")){
			boolean value = (Boolean)newValue;
			editor.putBoolean("showptt", value);
			editor.commit();
			return true;
		}
		return false;
	}



	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		if(id == R.id.back_up_icon) 
		{
			this.finish();
		}
	}

}
